package KoeDownload;

use strict;
use warnings;
use Carp qw(croak);
use WebService::Simple;
use LWP::Simple;
use JSON qw/decode_json/;

binmode STDOUT, ":utf8";

sub new {
    my $class = shift;
    my $args  = shift;
    my $chkstr = "";
    if ( $args->{url} =~ /http:\/\/koebu\.com\/api\/koe\/.*/ ){
        print "match URL\n\n"; 
    }else{
        croak "this URL dame zettai!";
    }
    my $self  = bless {
        url      => $args->{url},
        res      => '',
        data     => '',
        filename => '',
    } , $class;
    return $self;
}

sub save {
    my $self = shift;
    my $filename = shift;
    $self->{ filename } = $filename;
#   my  $self  = shift @_;
#   my ( $self ) = @_;
    my $mp3 = $self->mp3;
    my $topic_image = get("$mp3");
    open(OUT,">$filename");
    print OUT $topic_image;
    close(OUT);
}

sub url {
    my $self = shift;
    return $self->{url};
}

sub mp3 {
    my $self = shift;
    my $url = $self->{url};
    $url =~ s/^.+\/(.+?)$/$1/;

    my $mp3 = 'http://koebu.s3.amazonaws.com/mp3/'.join('/',reverse $url =~ /(((.).)..)/ ).'/'.$url.'.mp3';
    return $mp3;
}

sub filename { 
    my $self = shift;
    my $filename = $self->{ filename };
    return $filename; 
}

sub screen_name {
    my $self = shift;
    my $data = $self->data; 
    my $author_data = $data->{ author };
    my $screen_name = $author_data->{ screen_name };
    return $screen_name;
}
sub gender{
    my $self = shift;
    my $data = $self->data;
    my $gender = $data->{ gender };
    return $gender;
}
sub data{
    my $self = shift;

    return $self->{data} if $self->{data};

    my $res = $self->get_api_data;
    my $json = decode_json( $res->content );
    my $data = $json->{ result };

    $self->{data} = $data;

    return $data;
}
sub get_api_data{
    my $self = shift;

    return $self->{res} if $self->{res};

    my $service = WebService::Simple->new(
        base_url        => "$self->{ url }",
        response_parser => 'JSON'
    );
    my $res = eval {
        $service->get();
    };
    if ( $@ ) {
        croak $@;
    }
# see http://search.cpan.org/dist/WebService-Simple/lib/WebService/Simple/Response.pm
# , http://search.cpan.org/~gaas/HTTP-Message-6.02/lib/HTTP/Response.pm
    if ( $res->is_success ) {
        $self->{res} = $res;
    }
    else {
        print $res->status_line;
        print $res->message;
    }
    return $res;
}

#sub new{
#    my $class = shift;
#    my %args = @_ == 1 ? %{$_[0]} : @_;
#    my $self = bless { %args }, $class;
#    my $service = WebService::Simple->new(
#        base_url => "$self->{ url }",
#        response_parser => 'JSON'
#    );
#    my $res = $service->get();
#    my $json = decode_json( $res->content );
#    our $data = $json->{ result };
#    sub mp3($data){ "$data->{ mp3 }" }
#    my $image_url = $data->{ mp3 };
#    my $author_data = $data->{ author };
#    sub screen_name{ "$author_data->{ screen_name }" }
#    sub save{
#        my $topicImage = get("$image_url");
#        open(OUT,">/var/www/html/test/data/seikou.mp3");
#        print OUT $topicImage;
#        close(OUT);
#    }
#    return $self;
#}
#sub filename{"/var/www/html/test/data/hatumozyuru.mp3"};

1;
